from config import config
from g_share import g_share
from cstbnd import cstbnd
from lin_base import *

def check_shift_or_clip(cstbnddata):
    shift=False
    clip=False
    ra_degs = list(zip(*cstbnddata))[0]
    left_ra_deg= 18*15
    if min(ra_degs) < left_ra_deg:
        if max(ra_degs) > left_ra_deg:
            clip=True
        else:
            shift=True
    return shift, clip
            
def lin_oct_to_xyplot(form='dict'):
    from lin_oct_data import lin_oct_points
    xys=[]
    for ra_deg, dec in lin_oct_points:
        ra_hr = ra_deg / 15.0
        x = RA_to_x(g_share.x0, ra_hr)            
        y = dec_to_y(dec)
        if form=='dict':
            xys.append({'x':x, 'y':y})
        else:
            xys.append((x,y))

    return xys
    
def lin_umi_to_xyplot(form='dict'):
    from lin_umi_data import lin_umi_points
    xys=[]
    for ra_deg, dec in lin_umi_points:
        ra_hr = ra_deg / 15.0
        x = RA_to_x(g_share.x0, ra_hr)            
        y = dec_to_y(dec)
        if form=='dict':
            xys.append({'x':x, 'y':y})
        else:
            xys.append((x,y))

    return xys
    
def lin_cstbnd_to_xyplot(cst, form='dict', totalshift=False, excludes=['Oct','UMi']):
    if cst in excludes:
        return []
        
    if cst=='Oct':
        return lin_oct_to_xyplot(form=form)
        
    if cst =='UMi':
        return lin_umi_to_xyplot(form=form)
    
    cstbnddata = cstbnd[cst]
    #s=1
    #dec0=-1000
    #ra0=-1000
    xys=[]
    
    if not totalshift:
        shift, clip = check_shift_or_clip(cstbnddata)
    for ra_deg,dec in cstbnddata:
        ra_hr = ra_deg / 15.0
        if totalshift:
            ra_hr+=24
        else:
            if shift:
                ra_hr += 24
            if clip:
                if ra_hr < 18:
                    ra_hr = 18
        # clip right side
        if ra_hr > 23+24:
            ra_hr = 23+24
        x = RA_to_x(g_share.x0, ra_hr)            
        y = dec_to_y(dec)
        if form=='dict':
            xys.append({'x':x, 'y':y})
        else:
            xys.append((x,y))

    return xys
